package com.marsrover;

import java.util.ArrayList;
import java.util.List;

import com.marsrover.state.RoverManagerState;

public class RoverManager {
	private RoverManagerState state = RoverManagerState.READY_TO_BUILDER_ROVER;
	private Plateau plateau;
	private Rover currentRover;
	private List<Rover> launchedRovers = new ArrayList<Rover>();

	private RoverManager(Plateau plateau) {
		this.plateau = plateau;
	}

	/**
	 * It will create an instance of RoverManager which control rovers work on a rectangular plateau.
	 *
	 * @param upperRightCoordinates The upper right coordinates of the rectangular plateau.
	 * @return RoverManager
	 */
	public static RoverManager createRectangularPlateauRoverManager(String upperRightCoordinates) {
		return new RoverManager(new RectangularPlateau(upperRightCoordinates));
	}

	public Rover buildRover(String initLocationInfo) {
		return state.buildRover(this, initLocationInfo);
	}

	public void sendCommands(String commands) {
		state.sendCommands(this, commands);
	}

	public void launchRover() {
		state.launchRover(this);
	}

	public void reportLaunchedRoversInfo() {
		for (Rover rover : launchedRovers) {
			System.out.println(rover);
		}
	}

	public void setState(RoverManagerState state) {
		this.state = state;
	}

	public class Executor {
		public Rover buildRover(String initLocationInfo) {
			currentRover = new Rover(plateau, new Location(initLocationInfo));
			return currentRover;
		}

		public void sendCommands(String commands) {
			currentRover.setCommands(commands);
		}

		public void launchRover() {
			currentRover.executeCommands();
			launchedRovers.add(currentRover);
		}
	}
}
